<template>
  <div class="publish-container">
    <!--面包导航区域  -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/login' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/bannerManage'}">轮播图管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加轮播图</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 标题 -->
      <div slot="header" class="clearfix">
        <span>添加轮播图</span>
      </div>
      <!-- 表单 -->
      <el-form ref="addBannerRef" :model="banner" :rules="addFormRules" label-width="80px" label-position="left"  >
        <!-- 标题 作者区域 -->
        <el-row :gutter="60" type="flex" style="flex-direction: column;">
          <el-col :span="10">
            <el-form-item label="标题" prop="title">
              <el-input v-model="banner.title"  show-word-limit></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="英文" prop="EnglishName">
              <el-input v-model="banner.EnglishName"  show-word-limit></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="描述" prop="describe">
              <el-input v-model="banner.describe"  show-word-limit></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="按钮名称" prop="btnName">
              <el-input v-model="banner.btnName"  show-word-limit></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="链接" prop="href">
              <el-input v-model="banner.href"  show-word-limit></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="封面" >
          <el-radio-group v-model="banner.img" class="upload-img">
            <el-upload
              class="avatar-uploader"
              action="http://47.119.123.95:3000/banner/img"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="banner.img" :src="banner.img" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button  @click="$router.go(-1)">返回</el-button>
          <el-button type="primary" @click="onPublish(false)">添加</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

import {postAddArticle} from '@/api/banner.js'

export default {
  name:'AddBanner',
  data() {
    return {
      //添加轮播图参数
      banner: {
        title: "",
        img: "",
        tag: "",
        EnglishName: null,
        describe: null,
        btnName:null,
        href:""
      },
      addFormRules:{
        title:[
          {required: true, message: '请输入标题', trigger: 'blur'}
        ],
        EnglishName:[
          {required: true, message: '请输入英文名', trigger: 'blur'}
        ],
        describe:[
          {required: true, message: '请输入描述', trigger: 'blur'}
        ],
        btnName:[
          {required: true, message: '请输入按钮名称', trigger: 'blur'}
        ],
        href:[
          {required: true, message: '请输入链接地址', trigger: 'blur'}
        ]
      },
    };
  },
  computed: {},
  methods: {
    //添加轮播图
    async onPublish() {
      this.$refs.addBannerRef.validate(async (valid) =>{
        if(!valid) return
        const { data: res } = await postAddArticle(this.banner);
        if (res.meta.status !== 200) return this.$message.error("添加轮播图失败");
        this.$message.success("添加轮播图成功");
        // console.log(this.$router)
        this.$router.push("/bannerManage");
      })
    },
    handleAvatarSuccess(res, file) {
      this.banner.img = res.url;
      console.log(file)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

  },
  created() {},
  mounted() {},
};
</script>

<style  scoped>
.upload-img{
  border: 1px dashed;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>